keep = ls()

##############################
#Append Adjutant General Data#
##############################

states = c('CONNECTICUT', 'ILLINOIS', 'INDIANA', 'IOWA', 'MAINE', 'MASSACHUSETTS','VERMONT', 'WISCONSIN')
veterans = fread('./cleaned/agg_county_enlistment.csv')

#Veterans
veterans[, state_1860 := toupper(state)]
veterans = veterans[state_1860 %in% states]
veterans[, county_1860 := county]

################################################
#Add administrative data from Illinois, Indiana#
################################################

#Illinois
illinoisData = fread('./raw/illinois_enlistments_adj_gen.csv')
illinoisData[, county_1860 := toupper(County)]
illinoisData[, state_1860 := 'ILLINOIS']
illinoisData = illinoisData[, list(state_1860, county_1860, veterans_alt = veteransAdmin)]

#Indiana
indianaData = fread('./raw/indiana_enlistments_adj_gen.csv', nrows = 92, drop = 'V23')
indianaData[, county_1860 := toupper( county)]
indianaData[county_1860 %in% 'DEKALB', county_1860 := 'DE KALB']
indianaData[county_1860 %in% 'LAPORTE', county_1860 := 'LA PORTE']
indianaData[, state_1860 := 'INDIANA']
indianaData[, veterans_alt := volunteersService_Sept1862 + 
              volunteers_thru_1864 + 
              drafted_thru_1864 +
              volunteers_1865 +
              draft_1865]

indianaData = indianaData[, list(state_1860, county_1860, veterans_alt)]

#Append Adjutant General Data
setkey(illinoisData, state_1860, county_1860)
setkey(veterans, state_1860, county_1860)
veterans = illinoisData[veterans]
veterans[is.na(veterans_alt), veterans_alt := veterans]

veterans = rbindlist(list(veterans, indianaData), use.names = T, fill = T)
veterans = veterans[!(state_1860 %in% "INDIANA" & county_1860 %in% "LAKE" & !is.na(veterans)),]

fwrite(veterans, "./cleaned/county_enlistment_use.csv", row.names = F)

#Cleanup
rm(list = setdiff(ls(), c(keep, 'keep')))
gc()